// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <VisitInteractiveTool.h>
#include <vtkCamera.h>
#include <vtkRenderer.h>

// ****************************************************************************
//  Method:  VisitInteractiveTool::ComputeDisplayToWorld
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Brad Whitlock
//  Creation:    October  9, 2001
//
// ****************************************************************************
void
VisitInteractiveTool::ComputeDisplayToWorld(double x, double y, double z,
    double *worldPt)
{
    vtkRenderer *ren = proxy.GetCanvas();
    ren->SetDisplayPoint(x, y, z);
    ren->DisplayToWorld();
    ren->GetWorldPoint(worldPt);
    if(worldPt[3])
    {
        worldPt[0] /= worldPt[3];
        worldPt[1] /= worldPt[3];
        worldPt[2] /= worldPt[3];
        worldPt[3] = 1.0;
    }
}

// ****************************************************************************
//  Method:  VisitInteractiveTool::ComputeWorldToDisplay
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Brad Whitlock
//  Creation:    October  9, 2001
//
// ****************************************************************************
void
VisitInteractiveTool::ComputeWorldToDisplay(double x, double y, double z,
    double *displayPt)
{
    vtkRenderer *ren = proxy.GetCanvas();
    ren->SetWorldPoint(x, y, z, 1.0);
    ren->WorldToDisplay();
    ren->GetDisplayPoint(displayPt);
}

// ****************************************************************************
//  Method:  VisitInteractiveTool::ComputeDisplayToWorld
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    October  9, 2001
//
// ****************************************************************************
avtVector
VisitInteractiveTool::ComputeDisplayToWorld(const avtVector &v)
{
    double p[4];
    ComputeDisplayToWorld(v.x, v.y, v.z, p);
    return avtVector(p[0], p[1], p[2]);
}

// ****************************************************************************
//  Method:  VisitInteractiveTool::ComputeWorldToDisplay
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    October  9, 2001
//
// ****************************************************************************
avtVector
VisitInteractiveTool::ComputeWorldToDisplay(const avtVector &v)
{
    double p[4];
    ComputeWorldToDisplay(v.x, v.y, v.z, p);
    return avtVector(p[0], p[1], p[2]);
}

// ****************************************************************************
//  Method:  VisitInteractiveTool::DisplayToUnit
//
//  Purpose:
//    
//
//  Arguments:
//    
//
//  Programmer:  Jeremy Meredith
//  Creation:    October  9, 2001
//
// ****************************************************************************
avtVector
VisitInteractiveTool::DisplayToUnit(const avtVector &d)
{
    avtVector u;

    vtkRenderer *ren = proxy.GetCanvas();
    int *size = ren->GetSize();

    u.x = (2.0 * double(d.x)) / size[0]  -  1.0;
    u.y = (2.0 * double(d.y)) / size[1]  -  1.0;
    return u;
}

// ****************************************************************************
// Method: VisitInteractiveTool::FacingAxis
//
// Purpose: 
//   Returns the axis that is most facing the camera.
//
// Returns:    0,1 = X-axis, 2,3 = Y-axis, 4,5 = Z-axis
//
// Programmer: Brad Whitlock
// Creation:   Wed Jun 19 13:44:37 PST 2002
//
// Modifications:
//   
// ****************************************************************************

int
VisitInteractiveTool::FacingAxis()
{
    // Create a camera vector
    vtkCamera *camera = proxy.GetCanvas()->GetActiveCamera();
    const double *pos = camera->GetPosition();
    const double *focus = camera->GetFocalPoint();
    avtVector camvec(pos[0]-focus[0],pos[1]-focus[1],pos[2]-focus[2]);
    camvec.normalize();

    double dots[6];
    // X-axis
    dots[0] = camvec * avtVector(1., 0., 0.);
    dots[1] = camvec * avtVector(-1., 0., 0.);
    // Y-axis
    dots[2] = camvec * avtVector(0., 1., 0.);
    dots[3] = camvec * avtVector(0., -1., 0.);
    // Z-axis
    dots[4] = camvec * avtVector(0., 0., 1.);
    dots[5] = camvec * avtVector(0., 0., -1.);

    // Find the index of the largest dot product.
    int largestDotIndex = 0;
    for(int i = 1; i < 6; ++i)
    {
        if(dots[i] > dots[largestDotIndex])
            largestDotIndex = i;
    }

    return largestDotIndex;
}

// ****************************************************************************
// Method: VisitInteractiveTool::ComputeDepthTranslationDistance
//
// Purpose: 
//   Computes a good distance to depth translate.
//
// Returns:    A vector that is the increment that we can translate an object
//             so we can move it in and out along the current depth axis.
//
// Programmer: Brad Whitlock
// Creation:   Wed Oct 30 09:34:38 PDT 2002
//
// Modifications:
//   
// ****************************************************************************

avtVector
VisitInteractiveTool::ComputeDepthTranslationDistance()
{
    avtVector retval;
    int *size = proxy.GetCanvas()->GetSize();
    double bounds[6];
    proxy.GetBounds(bounds);
    double dX = bounds[1] - bounds[0];
    double dY = bounds[3] - bounds[2];
    double dZ = bounds[5] - bounds[4];

    switch(FacingAxis())
    {
    case 0:
        retval = avtVector(-1.,0.,0.) * (dX / double(size[1]));
        break;
    case 1:
        retval = avtVector(1.,0.,0.) * (dX / double(size[1]));
        break;
    case 2:
        retval = avtVector(0.,-1.,0.) * (dY / double(size[1]));
        break;
    case 3:
        retval = avtVector(0.,1.,0.) * (dY / double(size[1]));
        break;
    case 4:
        retval = avtVector(0.,0.,-1.) * (dZ / double(size[1]));
        break;
    case 5:
    default:
        retval = avtVector(0.,0.,1.) * (dZ / double(size[1]));
        break;
    }

    return retval;
}
